package com.atguigu.factory.simplefactory.order;

import com.atguigu.factory.simplefactory.pizza.CheesePizza;
import com.atguigu.factory.simplefactory.pizza.GreekPizza;
import com.atguigu.factory.simplefactory.pizza.Pizza;

import javax.sound.midi.SoundbankResource;
import java.io.*;

/**
 * @author LYJ
 * @Description
 * @date 2021 年 09 月 09 日 下午8:15
 */
class OrderPizza {

    private SimpleFactory simpleFactory;

    public OrderPizza(SimpleFactory simpleFactory) {
        this.setFactory(simpleFactory);
    }

    private void setFactory(SimpleFactory simpleFactory) {
        this.simpleFactory = simpleFactory;
        do {
            String type = getType();
            Pizza pizza = this.simpleFactory.createPizza(type);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("没有要创建的披萨");
            }
        } while (true);
    }

    //通过静态方法创建
    public OrderPizza(){
        do {
            String type = getType();
            Pizza pizza = SimpleFactory.createPizza2(type);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("没有要创建的披萨");
            }
        } while (true);
    }


    private String getType() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入定制的PIZZA种类:");
            return reader.readLine();
        } catch (IOException e) {
            System.out.println("读取Pizza出错:");
            return null;
        }
    }
}
